package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.GatewayRoute;
import com.tebiecloud.base.server.service.GatewayRouteService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 开放网关-路由 前端控制器
 * @author tebie
 * @date 2019-08-02
 */
@RestController
@RequestMapping("gatewayroute")
public class GatewayRouteController {

    @Autowired
    private GatewayRouteService gatewayRouteService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<GatewayRoute>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        GatewayRoute query = pageParams.mapToObject(GatewayRoute.class);
        QueryWrapper<GatewayRoute> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(gatewayRouteService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<GatewayRoute> get(@RequestParam("id") Long id) {
        GatewayRoute entity = gatewayRouteService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param routeName 路由名称,
     * @param path 路径,
     * @param serviceId 服务ID,
     * @param url 完整地址,
     * @param stripPrefix 忽略前缀,
     * @param retryable 0-不重试 1-重试,
     * @param status 状态:0-无效 1-有效,
     * @param isPersist 是否为保留数据:0-否 1-是,
     * @param routeDesc 路由说明
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "routeName", required = false) String routeName,
        @RequestParam(value = "path", required = false) String path,
        @RequestParam(value = "serviceId", required = false) String serviceId,
        @RequestParam(value = "url", required = false) String url,
        @RequestParam(value = "stripPrefix", required = false) Integer stripPrefix,
        @RequestParam(value = "retryable", required = false) Integer retryable,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "isPersist", required = false) Integer isPersist,
        @RequestParam(value = "routeDesc", required = false) String routeDesc
    ) {
        GatewayRoute entity = new GatewayRoute();
        entity.setRouteName(routeName);
        entity.setPath(path);
        entity.setServiceId(serviceId);
        entity.setUrl(url);
        entity.setStripPrefix(stripPrefix);
        entity.setRetryable(retryable);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setRouteDesc(routeDesc);
        gatewayRouteService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param routeName 路由名称
     * @param path 路径
     * @param serviceId 服务ID
     * @param url 完整地址
     * @param stripPrefix 忽略前缀
     * @param retryable 0-不重试 1-重试
     * @param status 状态:0-无效 1-有效
     * @param isPersist 是否为保留数据:0-否 1-是
     * @param routeDesc 路由说明
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "routeId", required = false) Long routeId,
            @RequestParam(value = "routeName", required = false) String routeName,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "serviceId", required = false) String serviceId,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "stripPrefix", required = false) Integer stripPrefix,
            @RequestParam(value = "retryable", required = false) Integer retryable,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "isPersist", required = false) Integer isPersist,
            @RequestParam(value = "routeDesc", required = false) String routeDesc
    ) {
        GatewayRoute entity = new GatewayRoute();
        entity.setRouteId(routeId);
        entity.setRouteName(routeName);
        entity.setPath(path);
        entity.setServiceId(serviceId);
        entity.setUrl(url);
        entity.setStripPrefix(stripPrefix);
        entity.setRetryable(retryable);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setRouteDesc(routeDesc);
        gatewayRouteService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            gatewayRouteService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            gatewayRouteService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
